/*
* $Id: CommonFunction.cpp 153 2010-09-26 17:33:07Z volok-aleksej@yandex.ru $
*
* Copyright 2008 by Volokitin Alexey
* All rights reserved
*
*/
#include "../include.h"
#include "CommonFunction.h"
#include <shlwapi.h>
#include <pcre.h>

CFont* CCommonFunction::GetFont(const std::tstring& strFontName, int sizeFont, bool bBold, bool bItalic, bool bUnderLine){
	LOGFONT lf;
	memset(&lf, 0, sizeof(lf));
	lf.lfHeight = sizeFont;
	lf.lfWeight = bBold ? FW_BOLD : FW_NORMAL;
	lf.lfCharSet = ANSI_CHARSET;
	lf.lfItalic = bItalic;
	lf.lfUnderline = bUnderLine;
	_tcscpy_s(lf.lfFaceName, LF_FACESIZE, strFontName.c_str());
	CFont* font = new CFont();
	font->CreateFontIndirect(&lf);
	return font;
}

COLORREF CCommonFunction::GetBeeColor(){
	return RGB(221, 165, 54);
}

std::tstring CCommonFunction::GetRegValue(const std::tstring& strNameKey, const std::tstring& strNameValue){
	std::tstring strValue;
	HKEY key;
	BOOL bRes = RegOpenKeyEx(HKEY_CURRENT_USER, strNameKey.c_str(), 0, KEY_QUERY_VALUE, &key);
	if(bRes == ERROR_SUCCESS){
		DWORD    cValues;
		RegQueryInfoKey(key, NULL, NULL, NULL, NULL, NULL, NULL, &cValues, NULL, NULL, NULL, NULL);
		DWORD    cchMaxValue;
		DWORD    cbMaxValueData;
		for (DWORD i = 0; i <= cValues; i++){
			cchMaxValue = 255;
			cbMaxValueData = 16383;
			TCHAR* value = new TCHAR[cbMaxValueData];
			TCHAR* valueName = new TCHAR[cchMaxValue];
			memset(value, 0, cbMaxValueData * sizeof(TCHAR));
			memset(valueName, 0, cchMaxValue * sizeof(TCHAR));
			int nres = RegEnumValue(key, i, valueName, &cchMaxValue, NULL, NULL, (LPBYTE)value, &cbMaxValueData);
			if(strNameValue == valueName)
				strValue = value;
			delete value;
			delete valueName;
		}
	}
	RegCloseKey(key);
	return strValue;
}

void CCommonFunction::SetRegValue(const std::tstring& strNameKey, const std::tstring& strNameValue, const std::tstring& strValue){
	HKEY key;
	BOOL bRes = RegOpenKeyEx(HKEY_CURRENT_USER, strNameKey.c_str(), 0, KEY_WRITE, &key);
	if(bRes != ERROR_SUCCESS){
		DWORD nDispos;
		RegCreateKeyEx(HKEY_CURRENT_USER, strNameKey.c_str(), NULL, NULL,
			REG_OPTION_NON_VOLATILE, KEY_CREATE_SUB_KEY | KEY_WRITE, NULL, &key, &nDispos);
	}
	RegSetValueEx(key, strNameValue.c_str(), NULL, REG_SZ, (LPBYTE)strValue.c_str(), DWORD(strValue.size() * sizeof(TCHAR)));
	RegCloseKey(key);
}

bool CCommonFunction::ValidRegex(const std::tstring& strValue, const std::tstring& strRegex){
	USES_CONVERSION;
	pcre *re;
	const char *error;
	int erroffset;
	re  =  pcre_compile(T2A(strRegex.c_str()), 0, &error, &erroffset, NULL);
	if(!re){
		return false;}
	else
	{
		int ovector[2];
		int count = pcre_exec(re,  NULL, (char *)T2A(strValue.c_str()), (int)strValue.size(), 0, 0, ovector, 2);
		if(count != 1 || ovector[0] != 0 || ovector[1] != strValue.size() || strValue.empty()){
			return false;}
	}
	return true;
}

CSize CCommonFunction::GetSizeYText(std::tstring& strText, int cx, CFont* pFont){
	CSize size(cx, 0);
	int szCx = 0;
	HDC dc = GetDC(GetDesktopWindow());
	HGDIOBJ oldFont = NULL;
	if(pFont)
		oldFont = SelectObject(dc, pFont->m_hObject);
	std::vector<std::tstring> aString = CStringFunction::GetStringArray(strText, _T("\n"));
	if(cx > 0){
		for(std::vector<std::tstring>::iterator it1 = aString.begin(); it1 != aString.end(); it1++){
			std::vector<std::tstring> aString1 = CStringFunction::GetStringArray(*it1, _T(" "));
			int fontSize = 0;
			for(std::vector<std::tstring>::iterator it = aString1.begin(); it != aString1.end(); it++){
				CSize size_temp;
				GetTextExtentPoint32(dc, (*it + _T(" ")).c_str(), (int)it->size() + 1, &size_temp);
				fontSize = size_temp.cy;
				if(szCx + size_temp.cx > cx){
					szCx = 0;
					size.cy += size_temp.cy;
				}else
					szCx += size_temp.cx;
			}
			size.cy += fontSize;
		}
	}else{
		for(std::vector<std::tstring>::iterator it = aString.begin(); it != aString.end(); it++){
			CSize size_temp;
			GetTextExtentPoint32(dc, it->c_str(), (int)it->size(), &size_temp);
			size.cy += size_temp.cy;
			if(size_temp.cx > cx)
				size.cx = (int)size_temp.cx;
		}
	}
	if(pFont)
		SelectObject(dc, oldFont);
	DeleteDC(dc);
	return size;
}

std::tstring CCommonFunction::GetStringType(Family::type nType){
	switch(nType){
		case Family::PEDIGREE:
			return ILanguageManager::GetLanguageManager()->GetString(TYPE_PEDIGREE_ID);
		case Family::BREEDING:
			return ILanguageManager::GetLanguageManager()->GetString(TYPE_BREEDING_ID);
		case Family::FATHER:
			return ILanguageManager::GetLanguageManager()->GetString(TYPE_FATHER_ID);
		case Family::AMADOU:
			return ILanguageManager::GetLanguageManager()->GetString(TYPE_AMADOU_ID);
		case Family::BARREN:
			return ILanguageManager::GetLanguageManager()->GetString(TYPE_BARREN_ID);
		case Family::QUEEN_CELL:
			return ILanguageManager::GetLanguageManager()->GetString(TYPE_QUEEN_CELL_ID);
		case Family::QUEEN_CELL_SMALL:
			return ILanguageManager::GetLanguageManager()->GetString(TYPE_QUEEN_CELL_SMALL_ID);
		case Family::INDIVIDUAL_PROVINE:
			return ILanguageManager::GetLanguageManager()->GetString(TYPE_INDIVIDUAL_PROVINE_ID);
		case Family::COMBINED_PROVINE:
			return ILanguageManager::GetLanguageManager()->GetString(TYPE_COMBINED_PROVINE_ID);
		case Family::PACKET:
			return ILanguageManager::GetLanguageManager()->GetString(TYPE_PACKET_ID);
		case Family::NON_QUEEN:
			return ILanguageManager::GetLanguageManager()->GetString(TYPE_NON_QUEEN_ID);
		case Family::SWARM:
			return ILanguageManager::GetLanguageManager()->GetString(TYPE_SWARM_ID);
		case Family::USER:
			return ILanguageManager::GetLanguageManager()->GetString(TYPE_USER_ID);
		default:
			return ILanguageManager::GetLanguageManager()->GetString(TYPE_DEFAULT_ID);
	}
}